package com.hrbxlh.util;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：DateUtil.java
* Description：时间处理util类。
* History：
* Date           Author               Desc
* 2017.4.5		  xzx                 建立
* -------------------------------------------------------
*/
public class DateUtil {

	/***
	 * @description 给当前日期，获取该日期的一星期的全部日期
	 * @param 要获取的条件日期
	 * @return List
	 * @throws Exception
	 */
	public static List<String> dateToWeek(Date paramDate) {  
		//判断某个日期是不是周日
		SimpleDateFormat sdfweek = new SimpleDateFormat("EEEE");  
		String week = sdfweek.format(paramDate);
		if(week.equals("星期日")){
			//如果某个日期是周日，就获取周六的日期去得到一周的数据
		    Calendar cal = Calendar.getInstance();  
		 	SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");
		    cal.setTime(paramDate);  
		    cal.add(Calendar.DAY_OF_MONTH, -1);  
		    Date date = cal.getTime();
		    paramDate = date;
		}
	    int b = paramDate.getDay();   
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd EEEE",Locale.CHINESE);  
	    List<String> list = new ArrayList<String>();  
	    Long fTime = paramDate.getTime() - b * 24 * 3600000;
	    Date fdate; 
	    for (int a = 1; a <= 7; a++) {  
	        fdate = new Date();  
	        fdate.setTime(fTime + (a * 24 * 3600000));  
	        list.add(a-1, sdf.format(fdate));  
	    }  
	    return list;  
	}  

	//eg:
	public static void main(String[] args) throws Exception {  
		String ddd = "2017-06-03";
	    // 定义输出日期格式  
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
	    List<String> days = dateToWeek(sdf.parse(ddd));  
	    System.out.println("今天的日期: " + ddd);  
	    for (String date : days) {  
	        System.out.println(date);  
	    } 
		/*
		DateUtil dd = new DateUtil();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startDate", "2017-06-02");
		map.put("endDate", "2017-08-02");
		List<Integer> list = new ArrayList<Integer>();
		list.add(1);
		list.add(4);
		map.put("weekList", list);
		Map<String, Object> ll = dd.getWeekByDate(map);
		System.out.println("aaaaaaaaaaaaaaaaaa:"+ll.get("dateList"));
	*/
	}  
	
	/**
	 * @description  通过时间段获取本时间段内的包含这个条件的星期的日期）  星期几的集合List<Integer>
	 * @param startDate 开始时间（2017-01-01）  
	 * 		  endDate   结束时间（2017-01-01）  
	 *        星期集合 weekList List<Integer> (1-7)
	 * @return  星期几为key value是这个星期的集合信息
	 * 			全部日期的集合key为dateList value为全部想要的日期
	 * @throws Exception
	 */
	public Map<String, Object> getWeekByDate(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> allList = new ArrayList<String>();
		try{
			//获取时间段
			String startDate = (String)paramMap.get("startDate");
			String endDate = (String)paramMap.get("endDate");
			//获取星期集合
			List<Integer> weekList = (List<Integer>)paramMap.get("weekList");
			Calendar c_begin = new GregorianCalendar();
			Calendar c_end = new GregorianCalendar();
			DateFormatSymbols dfs = new DateFormatSymbols();
			String[] weeks = dfs.getWeekdays();
			//开始时间月份由0-11
			c_begin.set(Integer.parseInt(startDate.split("-")[0]), Integer.parseInt(startDate.split("-")[1])-1, Integer.parseInt(startDate.split("-")[2])); 
			//结束时间月份由0-11
			c_end.set(Integer.parseInt(endDate.split("-")[0]), Integer.parseInt(endDate.split("-")[1])-1, Integer.parseInt(endDate.split("-")[2])); 
			//结束日期加一天（包含最后一天）
			c_end.add(Calendar.DAY_OF_YEAR, 1);  
			List<String> list ;
			for(int i = 0; i < weekList.size(); i++){
				//开始时间月份由0-11
				c_begin.set(Integer.parseInt(startDate.split("-")[0]), Integer.parseInt(startDate.split("-")[1])-1, Integer.parseInt(startDate.split("-")[2])); 
				//结束时间月份由0-11
				c_end.set(Integer.parseInt(endDate.split("-")[0]), Integer.parseInt(endDate.split("-")[1])-1, Integer.parseInt(endDate.split("-")[2])); 
				//结束日期加一天（包含最后一天）
				c_end.add(Calendar.DAY_OF_YEAR, 1);  
				list = new ArrayList<String>();
				//要获取的星期几
				//String weekNum = this.getWeekByDate(weekList.get(i));
				int weekNum = weekList.get(i);
				while(c_begin.before(c_end)){
					System.out.println("date:"+new java.sql.Date(c_begin.getTime().getTime()).toString()+ " xingqi:"+c_begin.get(Calendar.DAY_OF_WEEK));
//					if(weeks[c_begin.get(Calendar.DAY_OF_WEEK)].equals(weekNum)){
//						System.out.println("ccccccc:"+c_begin.get(Calendar.DAY_OF_WEEK)+"zhouji :"+weekNum);
//						System.out.println("week:"+weeks[c_begin.get(Calendar.DAY_OF_WEEK)]+"zhouji :"+weekNum);
//						list.add(new java.sql.Date(c_begin.getTime().getTime()).toString());
//					}
					if(c_begin.get(Calendar.DAY_OF_WEEK) != 1){
						if((c_begin.get(Calendar.DAY_OF_WEEK)-1) == weekNum){
							list.add(new java.sql.Date(c_begin.getTime().getTime()).toString());
						}
					}else{
						if((c_begin.get(Calendar.DAY_OF_WEEK) + 6) == weekNum){
							list.add(new java.sql.Date(c_begin.getTime().getTime()).toString());
						}
					}
					c_begin.add(Calendar.DAY_OF_YEAR, 1);
				}
				map.put(weekList.get(i).toString(), list);
				allList.addAll(list);
			}
			map.put("dateList", allList);
		}catch(Exception e){
			throw e;
		}
		return map;
	}
	
	/**
	 * @description  将数字转化为星期几 1-7 星期一-星期日
	 * @param 
	 * @return 
	 * @throws Exception
	 */
	public String getWeekByDate(int weekNum) throws Exception{
		
		String result = "";
		try{
			if(weekNum == 1){
				result="星期一";
			}else if(weekNum == 2){
				result="星期二";
			}else if(weekNum == 3){
				result="星期三";
			}else if(weekNum == 4){
				result="星期四";
			}else if(weekNum == 5){
				result="星期五";
			}else if(weekNum == 6){
				result="星期六";
			}else if(weekNum == 7){
				result="星期日";
			}
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
}
